throwing_ptr
Smart pointers that throw on dereference if null
Public Types | Public Member Functions | Friends | List of all members
throwing::unique_ptr< T[], Deleter > Class Template Reference

unique_ptr that manages a dynamically-allocated array of objects More...

Public Types

typedef std::unique_ptr< T[], Deleter > std_unique_ptr_type
 type of the wrapped std::unique_ptr. More...
 
typedef std::unique_ptr< T[], Deleter >::pointer pointer
 type of the pointer to the pointed objects. More...
 
typedef std::unique_ptr< T[], Deleter >::element_type element_type
 type of the pointed objects. More...
 
typedef std::unique_ptr< T[], Deleter >::deleter_type deleter_type
 type of the deleter. More...
 

Public Member Functions

TSP_CONSTEXPR unique_ptr () TSP_NOEXCEPT=default
 Constructs a throwing::unique_ptr that owns nothing. More...
 
TSP_CONSTEXPR unique_ptr (std::nullptr_t) TSP_NOEXCEPT
 Constructs a throwing::unique_ptr that owns nothing. More...
 
template<class U >
 unique_ptr (U ptr) TSP_NOEXCEPT
 Constructs a throwing::unique_ptr which owns p. More...
 
template<class U >
 unique_ptr (U ptr, typename std::conditional< std::is_reference< Deleter >::value, Deleter, const Deleter &>::type d1) TSP_NOEXCEPT
 Constructs a throwing::unique_ptr object which owns ptr. More...
 
template<class U >
 unique_ptr (U ptr, typename std::remove_reference< Deleter >::type &&d2) TSP_NOEXCEPT
 Constructs a throwing::unique_ptr object which owns ptr. More...
 
 unique_ptr (unique_ptr &&u) TSP_NOEXCEPT
 Constructs a unique_ptr by transferring ownership from u to *this. More...
 
template<class U , class E >
 unique_ptr (unique_ptr< U, E > &&u) TSP_NOEXCEPT
 Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E). More...
 
template<class U , class E >
 unique_ptr (std::unique_ptr< U, E > &&u) TSP_NOEXCEPT
 Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E). More...
 
 ~unique_ptr ()=default
 Destructor If get() == nullptr there are no effects. Otherwise, the owned object is destroyed via get_deleter()(get()) on the underlying unique_ptr. More...
 
unique_ptroperator= (unique_ptr &&r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class U , class E >
unique_ptroperator= (unique_ptr< U, E > &&r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class U , class E >
unique_ptroperator= (std::unique_ptr< U, E > &&r) TSP_NOEXCEPT
 Assignment from std::unique_ptr operator. More...
 
unique_ptroperator= (std::nullptr_t) TSP_NOEXCEPT
 Assignment operator. More...
 
pointer release () TSP_NOEXCEPT
 Releases the ownership of the managed object if any. More...
 
void reset (pointer ptr=pointer()) TSP_NOEXCEPT
 Replaces the managed object. More...
 
template<class U >
void reset (U ptr) TSP_NOEXCEPT
 Replaces the managed object. More...
 
void reset (std::nullptr_t ptr=nullptr) TSP_NOEXCEPT
 Equivalent to reset(pointer()) More...
 
void swap (unique_ptr &other) TSP_NOEXCEPT
 Swaps the managed objects and associated deleters of *this and another unique_ptr object other. More...
 
pointer get () const TSP_NOEXCEPT
 Returns a pointer to the managed object or nullptr if no object is owned. More...
 
Deleter & get_deleter () TSP_NOEXCEPT
 Returns the deleter object which would be used for destruction of the managed object. More...
 
const Deleter & get_deleter () const TSP_NOEXCEPT
 Returns the deleter object which would be used for destruction of the managed object. More...
 
 operator bool () const TSP_NOEXCEPT
 Checks whether *this owns an object, i.e. whether get() != nullptr. More...
 
T & operator[] (size_t i) const
 provides access to elements of an array managed by a unique_ptr. More...
 
std_unique_ptr_typeget_std_unique_ptr () TSP_NOEXCEPT
 Returns reference to the wrapped std::unique_ptr. More...
 
const std_unique_ptr_typeget_std_unique_ptr () const TSP_NOEXCEPT
 Returns const reference to the wrapped std::unique_ptr. More...
 

Friends

template<typename OtherT , typename OtherDeleter >
class unique_ptr
 

Detailed Description

template<typename T, typename Deleter>
class throwing::unique_ptr< T[], Deleter >

unique_ptr that manages a dynamically-allocated array of objects

throwing::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope.

The object is disposed of using the associated deleter when either of the following happens:

The object is disposed of using a potentially user-supplied deleter by calling get_deleter()(ptr). The default deleter uses the delete operator, which destroys the object and deallocates the memory.

A unique_ptr may alternatively own no object, in which case it is called empty.

Definition at line 328 of file unique_ptr.hpp.

Member Typedef Documentation

◆ deleter_type

template<typename T , typename Deleter >
typedef std::unique_ptr<T[], Deleter>::deleter_type throwing::unique_ptr< T[], Deleter >::deleter_type

type of the deleter.

Definition at line 337 of file unique_ptr.hpp.

◆ element_type

template<typename T , typename Deleter >
typedef std::unique_ptr<T[], Deleter>::element_type throwing::unique_ptr< T[], Deleter >::element_type

type of the pointed objects.

Definition at line 335 of file unique_ptr.hpp.

◆ pointer

template<typename T , typename Deleter >
typedef std::unique_ptr<T[], Deleter>::pointer throwing::unique_ptr< T[], Deleter >::pointer

type of the pointer to the pointed objects.

Definition at line 333 of file unique_ptr.hpp.

◆ std_unique_ptr_type

template<typename T , typename Deleter >
typedef std::unique_ptr<T[], Deleter> throwing::unique_ptr< T[], Deleter >::std_unique_ptr_type

type of the wrapped std::unique_ptr.

Definition at line 331 of file unique_ptr.hpp.

Constructor & Destructor Documentation

◆ unique_ptr() [1/8]

template<typename T , typename Deleter >
TSP_CONSTEXPR throwing::unique_ptr< T[], Deleter >::unique_ptr ( )
default

Constructs a throwing::unique_ptr that owns nothing.

Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.

◆ unique_ptr() [2/8]

template<typename T , typename Deleter >
TSP_CONSTEXPR throwing::unique_ptr< T[], Deleter >::unique_ptr ( std::nullptr_t  )
inline

Constructs a throwing::unique_ptr that owns nothing.

Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.

Definition at line 356 of file unique_ptr.hpp.

356 {}

◆ unique_ptr() [3/8]

template<typename T , typename Deleter >
template<class U >
throwing::unique_ptr< T[], Deleter >::unique_ptr ( ptr)
inlineexplicit

Constructs a throwing::unique_ptr which owns p.

Initialises the stored pointer with ptr and value-initialises the stored deleter.

Requires that Deleter is DefaultConstructible and that construction does not throw an exception.

This constructor is ill-formed if Deleter is of pointer or reference type. (until C++17)

This overload only participates in overload resolution if std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type. The program is ill-formed if this constructor is selected by class template argument deduction. (since C++17)

This overload will not participate in overload resolution unless one of the following is true:

  • U is the same type as pointer, or
  • U is std::nullptr_t, or
  • pointer is the same type as element_type* and U is some pointer type V* such that V(*)[] is implicitly convertible to element_type(*)[]

Definition at line 381 of file unique_ptr.hpp.

381 : p(ptr) {}

◆ unique_ptr() [4/8]

template<typename T , typename Deleter >
template<class U >
throwing::unique_ptr< T[], Deleter >::unique_ptr ( ptr,
typename std::conditional< std::is_reference< Deleter >::value, Deleter, const Deleter &>::type  d1 
)
inline

Constructs a throwing::unique_ptr object which owns ptr.

Initialises the stored pointer with ptr and initialises the stored deleter with d1

This overload will not participate in overload resolution unless one of the following is true:

  • U is the same type as pointer, or
  • U is std::nullptr_t, or
  • pointer is the same type as element_type* and U is some pointer type V* such that V(*)[] is implicitly convertible to element_type(*)[]

Definition at line 398 of file unique_ptr.hpp.

401  : p(ptr, std::forward<decltype(d1)>(d1)) {}

◆ unique_ptr() [5/8]

template<typename T , typename Deleter >
template<class U >
throwing::unique_ptr< T[], Deleter >::unique_ptr ( ptr,
typename std::remove_reference< Deleter >::type &&  d2 
)
inline

Constructs a throwing::unique_ptr object which owns ptr.

Initialises the stored pointer with ptr. Moves d2 into stored_deleter.

This overload will not participate in overload resolution unless one of the following is true:

  • U is the same type as pointer, or
  • U is std::nullptr_t, or
  • pointer is the same type as element_type* and U is some pointer type V* such that V(*)[] is implicitly convertible to element_type(*)[]

Definition at line 416 of file unique_ptr.hpp.

418  : p(ptr, std::forward<decltype(d2)>(d2)) {}

◆ unique_ptr() [6/8]

template<typename T , typename Deleter >
throwing::unique_ptr< T[], Deleter >::unique_ptr ( unique_ptr< T[], Deleter > &&  u)
inline

Constructs a unique_ptr by transferring ownership from u to *this.

If Deleter is not a reference type, requires that it is nothrow-MoveConstructible (if Deleter is a reference, get_deleter() and u.get_deleter() after move construction reference the same value)

Definition at line 427 of file unique_ptr.hpp.

427 : p(std::move(u.p)) {}

◆ unique_ptr() [7/8]

template<typename T , typename Deleter >
template<class U , class E >
throwing::unique_ptr< T[], Deleter >::unique_ptr ( unique_ptr< U, E > &&  u)
inline

Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E).

It depends upon whether E is a reference type, as following: a) if E is a reference type, this deleter is copy constructed from u's deleter (requires that this construction does not throw) b) if E is a non-reference type, this deleter is move constructed from u's deleter (requires that this construction does not throw)

This constructor only participates in overload resolution if all of the following are true: a) unique_ptr<U, E>::pointer is implicitly convertible to pointer b) U is an array type c) pointer is the same type as element_type* d) unique_ptr<U,E>::pointer is the same type as unique_ptr<U,E>::element_type* e) unique_ptr<U,E>::element_type(*)[] is convertible to element_type(*)[] f) Either Deleter is a reference type and E is the same type as Deleter, or Deleter is not a reference type and E is implicitly convertible to Deleter.

Definition at line 451 of file unique_ptr.hpp.

452  : p(std::move(u.get_std_unique_ptr())) {}

◆ unique_ptr() [8/8]

template<typename T , typename Deleter >
template<class U , class E >
throwing::unique_ptr< T[], Deleter >::unique_ptr ( std::unique_ptr< U, E > &&  u)
inline

Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E).

It depends upon whether E is a reference type, as following: a) if E is a reference type, this deleter is copy constructed from u's deleter (requires that this construction does not throw) b) if E is a non-reference type, this deleter is move constructed from u's deleter (requires that this construction does not throw)

This constructor only participates in overload resolution if all of the following are true: a) unique_ptr<U, E>::pointer is implicitly convertible to pointer b) U is an array type c) pointer is the same type as element_type* d) unique_ptr<U,E>::pointer is the same type as unique_ptr<U,E>::element_type* e) unique_ptr<U,E>::element_type(*)[] is convertible to element_type(*)[] f) Either Deleter is a reference type and E is the same type as Deleter, or Deleter is not a reference type and E is implicitly convertible to Deleter.

Definition at line 476 of file unique_ptr.hpp.

476 : p(std::move(u)) {}

◆ ~unique_ptr()

template<typename T , typename Deleter >
throwing::unique_ptr< T[], Deleter >::~unique_ptr ( )
default

Destructor If get() == nullptr there are no effects. Otherwise, the owned object is destroyed via get_deleter()(get()) on the underlying unique_ptr.

Requires that get_deleter()(get()) does not throw exceptions.

Member Function Documentation

◆ get()

template<typename T , typename Deleter >
pointer throwing::unique_ptr< T[], Deleter >::get ( ) const
inline

Returns a pointer to the managed object or nullptr if no object is owned.

Definition at line 615 of file unique_ptr.hpp.

615 { return p.get(); }

◆ get_deleter() [1/2]

template<typename T , typename Deleter >
Deleter& throwing::unique_ptr< T[], Deleter >::get_deleter ( )
inline

Returns the deleter object which would be used for destruction of the managed object.

Definition at line 620 of file unique_ptr.hpp.

620 { return p.get_deleter(); }

◆ get_deleter() [2/2]

template<typename T , typename Deleter >
const Deleter& throwing::unique_ptr< T[], Deleter >::get_deleter ( ) const
inline

Returns the deleter object which would be used for destruction of the managed object.

Definition at line 625 of file unique_ptr.hpp.

625 { return p.get_deleter(); }

◆ get_std_unique_ptr() [1/2]

template<typename T , typename Deleter >
std_unique_ptr_type& throwing::unique_ptr< T[], Deleter >::get_std_unique_ptr ( )
inline

Returns reference to the wrapped std::unique_ptr.

Definition at line 648 of file unique_ptr.hpp.

648 { return p; }

◆ get_std_unique_ptr() [2/2]

template<typename T , typename Deleter >
const std_unique_ptr_type& throwing::unique_ptr< T[], Deleter >::get_std_unique_ptr ( ) const
inline

Returns const reference to the wrapped std::unique_ptr.

Definition at line 652 of file unique_ptr.hpp.

652  {
653  return p;
654  }

◆ operator bool()

template<typename T , typename Deleter >
throwing::unique_ptr< T[], Deleter >::operator bool ( ) const
inlineexplicit

Checks whether *this owns an object, i.e. whether get() != nullptr.

Definition at line 630 of file unique_ptr.hpp.

630 { return p.operator bool(); }

◆ operator=() [1/4]

template<typename T , typename Deleter >
unique_ptr& throwing::unique_ptr< T[], Deleter >::operator= ( unique_ptr< T[], Deleter > &&  r)
inline

Assignment operator.

Transfers ownership from r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).

If Deleter is not a reference type, requires that it is nothrow-MoveAssignable.

If Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.

Definition at line 498 of file unique_ptr.hpp.

498  {
499  p = std::move(r.p);
500  return *this;
501  }

◆ operator=() [2/4]

template<typename T , typename Deleter >
template<class U , class E >
unique_ptr& throwing::unique_ptr< T[], Deleter >::operator= ( unique_ptr< U, E > &&  r)
inline

Assignment operator.

Transfers ownership from r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).

If Deleter is not a reference type, requires that it is nothrow-MoveAssignable.

If Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.

Only participates in overload resolution if all of the following is true:

Definition at line 524 of file unique_ptr.hpp.

524  {
525  p = std::move(r.p);
526  return *this;
527  }

◆ operator=() [3/4]

template<typename T , typename Deleter >
template<class U , class E >
unique_ptr& throwing::unique_ptr< T[], Deleter >::operator= ( std::unique_ptr< U, E > &&  r)
inline

Assignment from std::unique_ptr operator.

Transfers ownership from r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).

If Deleter is not a reference type, requires that it is nothrow-MoveAssignable.

If Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.

Only participates in overload resolution if all of the following is true:

Definition at line 550 of file unique_ptr.hpp.

550  {
551  p = std::move(r);
552  return *this;
553  }

◆ operator=() [4/4]

template<typename T , typename Deleter >
unique_ptr& throwing::unique_ptr< T[], Deleter >::operator= ( std::nullptr_t  )
inline

Assignment operator.

Effectively the same as calling reset().

Definition at line 559 of file unique_ptr.hpp.

559  {
560  p = nullptr;
561  return *this;
562  }

◆ operator[]()

template<typename T , typename Deleter >
T& throwing::unique_ptr< T[], Deleter >::operator[] ( size_t  i) const
inline

provides access to elements of an array managed by a unique_ptr.

The parameter i shall be less than the number of elements in the array; otherwise, the behavior is undefined.

Exceptions
throwing::null_ptr_exception<element_type>if the unique_ptr is empty

Definition at line 640 of file unique_ptr.hpp.

640  {
641  if (!p)
642  throw null_ptr_exception<element_type>();
643  return p[i];
644  }

◆ release()

template<typename T , typename Deleter >
pointer throwing::unique_ptr< T[], Deleter >::release ( )
inline

Releases the ownership of the managed object if any.

get() returns nullptr after the call.

Returns
Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be returned by get() before the call.

Definition at line 571 of file unique_ptr.hpp.

571 { return p.release(); }

◆ reset() [1/3]

template<typename T , typename Deleter >
void throwing::unique_ptr< T[], Deleter >::reset ( pointer  ptr = pointer())
inline

Replaces the managed object.

Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:

  • Saves a copy of the current pointer old_ptr = current_ptr
  • Overwrites the current pointer with the argument current_ptr = ptr
  • If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).

Definition at line 583 of file unique_ptr.hpp.

583 { p.reset(ptr); }

◆ reset() [2/3]

template<typename T , typename Deleter >
template<class U >
void throwing::unique_ptr< T[], Deleter >::reset ( ptr)
inline

Replaces the managed object.

Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:

  • Saves a copy of the current pointer old_ptr = current_ptr
  • Overwrites the current pointer with the argument current_ptr = ptr
  • If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).

Will only participate in overload resolution if either:

  • U is the same type as pointer, or
  • pointer is the same type as element_type* and U is a pointer type V* such that V(*)[] is convertible to element_type(*)[]. (available since C++17)

Definition at line 601 of file unique_ptr.hpp.

601 { p.reset(ptr); }

◆ reset() [3/3]

template<typename T , typename Deleter >
void throwing::unique_ptr< T[], Deleter >::reset ( std::nullptr_t  ptr = nullptr)
inline

Equivalent to reset(pointer())

Definition at line 605 of file unique_ptr.hpp.

605 { p.reset(ptr); }

◆ swap()

template<typename T , typename Deleter >
void throwing::unique_ptr< T[], Deleter >::swap ( unique_ptr< T[], Deleter > &  other)
inline

Swaps the managed objects and associated deleters of *this and another unique_ptr object other.

Definition at line 610 of file unique_ptr.hpp.

610 { p.swap(other.p); }

Friends And Related Function Documentation

◆ unique_ptr

template<typename T , typename Deleter >
template<typename OtherT , typename OtherDeleter >
friend class unique_ptr
friend

Definition at line 340 of file unique_ptr.hpp.


The documentation for this class was generated from the following file: